#include "BoundingBox.h"
#include <GL\glut.h>


BoundingBox::BoundingBox(float xmin,float ymin,float zmin,float xmax, float ymax, float zmax):
	xMin(xmin),
	xMax(xmax),
	yMin(ymin),
	yMax(ymax),
	zMin(zmin),
	zMax(zmax)
{
}

BoundingBox::BoundingBox(Vector3<float> origin, float width, float height, float depth):
	xMin(origin.X()),
	yMin(origin.Y()),
	zMin(origin.Z()),
	xMax(origin.X() + width),
	yMax(origin.Y() + height),
	zMax(origin.Z() + depth)
{
}

BoundingBox::BoundingBox(std::vector<Triangle> model):
	xMin(FLT_MAX),
	xMax(-FLT_MAX),
	yMin(FLT_MAX),
	yMax(-FLT_MAX),
	zMin(FLT_MAX),
	zMax(-FLT_MAX)
{

	for(int i = 0; i < model.size(); i++)
	{
		for(int j = 0; j < 3; j++)
		{
			if(model[i].Points[j].X() < xMin)
			{
				xMin = model[i].Points[j].X();
			}
			if(model[i].Points[j].X() > xMax)
			{
				xMax = model[i].Points[j].X();
			}

			if(model[i].Points[j].Y() < yMin)
			{
				yMin = model[i].Points[j].Y();
			}
			if(model[i].Points[j].Y() > yMax)
			{
				yMax = model[i].Points[j].Y();
			}
			
			if(model[i].Points[j].Z() < zMin)
			{
				zMin = model[i].Points[j].Z();
			}
			if(model[i].Points[j].Z() > zMax)
			{
				zMax = model[i].Points[j].Z();
			}
		}
	}
}

BoundingBox::~BoundingBox(void)
{
}

void BoundingBox::Translate(Vector3<float> distance)
{
	xMin += distance.X();
	xMax += distance.X();

	yMin += distance.Y();
	yMax += distance.Y();

	zMin += distance.Z();
	zMax += distance.Z();
}

float BoundingBox::Width()
{
	return xMax - xMin;
}
float BoundingBox::Height()
{
	return yMax - yMin;
}
float BoundingBox::Depth()
{
	return zMax - zMin;
}

Vector3<float> BoundingBox::Mins()
{
	return  Vector3<float>(xMin,yMin,zMin);
}

Vector3<float> BoundingBox::Maxes()
{
	return Vector3<float>(xMax,yMax,zMax);
}

// if the box passed in is  within the boundreis of this box 
// the function will return true 
bool BoundingBox::Contains(BoundingBox box)
{
	if(box.xMin > xMin && box.xMax < xMax &&
		box.yMin > yMin && box.yMax < yMax &&
		box.zMin > zMin && box.zMax < zMax)
	{
		return true;
	}
	return false;
}
// if the boxes are colideing this function will return true
bool BoundingBox::Touching(BoundingBox box)
{
	if(box.xMin < xMin || box.xMax > xMax ||
		box.yMin < yMin || box.yMax > yMax ||
		box.zMin < zMin || box.zMax > zMax)
	{
		return false;
	}
	return true;
}

void BoundingBox::Draw()
{
	glBegin(GL_LINE_STRIP); 
	glColor3f(1,0,1);
	
	glVertex3f(xMin, yMin, zMin);
	glVertex3f(xMax, yMin, zMin);
	glVertex3f(xMax, yMax, zMin);
	glVertex3f(xMin, yMax, zMin);
	glVertex3f(xMin, yMin, zMin);

	glVertex3f(xMin, yMin, zMax);
	glVertex3f(xMax, yMin, zMax);
	glVertex3f(xMax, yMin, zMin);
	glVertex3f(xMax, yMax, zMin);
	glVertex3f(xMax, yMax, zMax);
	glVertex3f(xMax, yMin, zMax);

	glVertex3f(xMax, yMax, zMax);
	glVertex3f(xMin, yMax, zMax);
	glVertex3f(xMin, yMax, zMin);
	glVertex3f(xMin, yMax, zMax);
	glVertex3f(xMin, yMin, zMax);
	

	glEnd();
}
